package com.bulletelc.apollopre.module.main;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.utils.AppUtil;
import com.bulletelc.apollopre.utils.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.io.File;

/**
 * author: lhl
 * date  : 2017/11/14
 * desc  : TODO
 */

public class MainModel implements MainContract.Model {

    @Override
    public void checkVersion(final AsyncCallback callback) {
        int currentVersion= AppUtil.getVersionCode(MyApplication.getInstance());
        OkGo.<String>post("http://www.zdt6.com/Home/App/checkApkVer")
                .params("ver", currentVersion)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtil.i(response.body().toString());
                        callback.onSuccess(null);
                    }
                });
    }

    @Override
    public void downloadAPK(final MainContract.MainModelCallback callback) {
        OkGo.<File>post("http://www.zdt6.com/Home/App/apkurl")
                    .execute(new FileCallback() {
                        @Override
                        public void onStart(Request<File, ? extends Request> request) {
                            super.onStart(request);
                            LogUtil.i("开始下载");
                            callback.onStart();
                        }

                        @Override
                        public void onSuccess(Response<File> response) {
                            LogUtil.i("下载完成");
                            callback.onSuccess(response);
                        }

                        @Override
                        public void onError(Response<File> response) {
                            super.onError(response);
                            callback.onError(response);
                        }

                        @Override
                        public void onFinish() {
                            super.onFinish();
                            LogUtil.i("下载完成");
                            callback.onFinish();
                        }

                        @Override
                        public void downloadProgress(Progress progress) {
                            super.downloadProgress(progress);
                            callback.downloadProgress(progress.currentSize,progress.totalSize, progress.fraction);
                        }
                    });
    }
}
